package com.zr.eim.service.impl;
import com.zr.eim.domain.EimDataCloseContactInfo;
import com.zr.eim.domain.EimDataPerson;
import com.zr.eim.mapper.EimDataCloseContactInfoMapper;
import com.zr.eim.mapper.EimDataPersonMapper;
import com.zr.eim.service.IEimDataDiagnosticChainService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 确诊传播链Service业务层处理
 *
 * @author why
 * @date 2022-10-28
 */
@Service
public class EimDataDiagnosticChainServiceImpl implements IEimDataDiagnosticChainService {


    @Resource
    private EimDataPersonMapper eimDataPersonMapper;

    @Resource
    EimDataCloseContactInfoMapper eimDataCloseContactInfoMapper;

    /**
     * 获取传播链信息
     * @param id 确诊人id
     * @return
     */
    @Override
    public EimDataPerson getChain(Long id) {
        //根据id查找该对象信息
        EimDataPerson person = eimDataPersonMapper.selectEimDataPersonByPersonId(id.toString());
        if(person != null ){
            setChildren(person);
        }
        return person;
    }

    /**
     * 递归方法，传入确诊对象，设置自身传播链的下一代children
     * @param person
     */
    public void setChildren(EimDataPerson person){
        //创建密接信息对象
        EimDataCloseContactInfo eimDataCloseContactInfo = new EimDataCloseContactInfo();
        //根据传入的person信息，设置密接信息对象的PersonId属性值
        eimDataCloseContactInfo.setDiagnosticPersonId(person.getPersonId());
        //根据该确诊密接信息对象，查询与该对象密接的密接信息列表
        List<EimDataCloseContactInfo> closeContactInfoList = eimDataCloseContactInfoMapper.selectEimDataCloseContactInfoList(eimDataCloseContactInfo);
        //遍历密接列表，如果密接人确诊，则将其赋值到自身的children属性中
        List<EimDataPerson> eimDataPersonList = new ArrayList<>();
        for(EimDataCloseContactInfo info : closeContactInfoList){
            //判断是否有密接人，该密接人是否确诊
            if(info.getPerson() !=null && info.getPerson().getIfDiagnosis() == 1){
                //将密接人插入到列表
                eimDataPersonList.add(info.getPerson());
                //将确诊的密接人继续递归
                setChildren(info.getPerson());
            }
        }
        //将获得的确诊的密接人列表赋给children属性
        person.setChildren(eimDataPersonList);

    }
}
